package jz.joyoung.robot.util;

/**
 * Created by lj on 2017/6/5.
 */

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by lj
 * 为了修改状态栏的文字颜色和背景
 * 在设置状态栏全透明后，整个activity布局都会上移充满整个屏幕，如果你不想让布局上移的话就需要在根布局设置
 * android:fitsSystemWindows="true"
 * http://www.tuicool.com/articles/2IFrmmm
 */
public class StatusBarUtil {

    /**
     * 设置状态栏全透明
     *
     * @param activity 需要设置的activity
     */
    public static void setTransparent(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }
        transparentStatusBar( activity );
        setRootView( activity );
    }

    /**
     * 设置根布局参数
     */
    private static void setRootView(Activity activity) {
        ViewGroup parent = activity.findViewById( android.R.id.content );
        for (int i = 0, count = parent.getChildCount(); i < count; i++) {
            View childView = parent.getChildAt( i );
            if (childView instanceof ViewGroup) {
                childView.setFitsSystemWindows( true );
                ((ViewGroup) childView).setClipToPadding( true );
            }
        }
    }

    /**
     * 使状态栏透明
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private static void transparentStatusBar(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().addFlags( WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS );
            activity.getWindow().clearFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
            //需要设置这个flag contentView才能延伸到状态栏
            activity.getWindow().addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION );
            //状态栏覆盖在contentView上面，设置透明使contentView的背景透出来
            activity.getWindow().setStatusBarColor( Color.TRANSPARENT );
        } else {
            //让contentView延伸到状态栏并且设置状态栏颜色透明
            activity.getWindow().addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
        }
    }


    /**
     * 修改状态栏为全透明
     *
     * @param activity
     */
    @TargetApi(19)
    public static void transparencyBar(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.clearFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION );
            window.getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE );
            window.addFlags( WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS );
            window.setStatusBarColor( Color.TRANSPARENT );
            window.setNavigationBarColor( Color.TRANSPARENT );
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = activity.getWindow();
            window.setFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
        }
    }


    /**
     * 设置状态栏黑色字体图标，
     * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
     *
     * @param activity
     * @return 1:MIUUI 2:Flyme 3:android6.0
     */
    public static int StatusBarLightMode(Activity activity) {
        int result = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // MIUISetStatusBarLightMode
            // MIUISetStatusBarLightMode
            if (MIUISetStatusBarLightMode( activity.getWindow(), true )) {
                result = 1;
            } else if (FlymeSetStatusBarLightMode( activity.getWindow(), true )) {
                result = 2;
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| 全屏 不需要
                activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR );
                result = 3;
            }
        }
        Log.e( "StatusBarLightMode---", result + "" );
        return result;
    }

    /**
     * 已知系统类型时，设置状态栏黑色字体图标。
     * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
     *
     * @param activity
     * @param type     1:MIUUI 2:Flyme 3:android6.0
     */
    public static void StatusBarLightMode(Activity activity, int type) {
        if (type == 1) {
            MIUISetStatusBarLightMode( activity.getWindow(), true );
        } else if (type == 2) {
            FlymeSetStatusBarLightMode( activity.getWindow(), true );
        } else if (type == 3) {
            //View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| 全屏 不需要
            activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR );
        }

    }

    /**
     * 已知系统类型时，设置状态栏黑色字体图标。
     * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
     */
    public static void setStatusBarLightMode(Window window) {
        int type = getStatusBarLightMode( window );
        if (type == 1) {
            MIUISetStatusBarLightMode( window, true );
        } else if (type == 2) {
            FlymeSetStatusBarLightMode( window, true );
        } else if (type == 3) {
            window.getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR );
        } else {//5.0

        }
    }

    /**
     * 设置状态栏黑色字体图标，
     * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
     *
     * @return 1:MIUUI 2:Flyme 3:android6.0
     */
    public static int getStatusBarLightMode(Window window) {
        int result = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (MIUISetStatusBarLightMode( window, true )) {
                result = 1;
            } else if (FlymeSetStatusBarLightMode( window, true )) {
                result = 2;
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR );
                result = 3;
            } else {//5.0

            }
        }
        return result;
    }


    /**
     * 清除MIUI或flyme或6.0以上版本状态栏黑色字体
     */
    public static void StatusBarDarkMode(Activity activity, int type) {
        if (type == 1) {
            MIUISetStatusBarLightMode( activity.getWindow(), false );
        } else if (type == 2) {
            FlymeSetStatusBarLightMode( activity.getWindow(), false );
        } else if (type == 3) {
            activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_VISIBLE );
        }

    }


    /**
     * 设置状态栏图标为深色和魅族特定的文字风格
     * 可以用来判断是否为Flyme用户
     *
     * @param window 需要设置的窗口
     * @param dark   是否把状态栏字体及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField( "MEIZU_FLAG_DARK_STATUS_BAR_ICON" );
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField( "meizuFlags" );
                darkFlag.setAccessible( true );
                meizuFlags.setAccessible( true );
                int bit = darkFlag.getInt( null );
                int value = meizuFlags.getInt( lp );
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt( lp, value );
                window.setAttributes( lp );
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

    /**
     * 设置状态栏字体图标为深色，需要MIUIV6以上
     *
     * @param window 需要设置的窗口
     * @param dark   是否把状态栏字体及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag = 0;
                Class layoutParams = Class.forName( "android.view.MiuiWindowManager$LayoutParams" );
                Field field = layoutParams.getField( "EXTRA_FLAG_STATUS_BAR_DARK_MODE" );
                darkModeFlag = field.getInt( layoutParams );
                Method extraFlagField = clazz.getMethod( "setExtraFlags", int.class, int.class );
                if (dark) {
                    extraFlagField.invoke( window, darkModeFlag, darkModeFlag );//状态栏透明且黑色字体
                } else {
                    extraFlagField.invoke( window, 0, darkModeFlag );//清除黑色字体
                }
                result = true;
                //新加的
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    //开发版 7.7.13 及以后版本采用了系统API，旧方法无效但不会报错，所以两个方式都要加上
                    if (dark) {
                        //View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| 全屏 不需要
                        window.getDecorView().setSystemUiVisibility(
                                View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR );
                    } else {
                        window.getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_VISIBLE );
                    }
                }
            } catch (Exception e) {

            }
        }
        return result;
    }


    //为 Activity 的状态栏设置颜色.
    @SuppressLint("ObsoleteSdkInt")
    public static void setStatusBarColor(Activity activity, int color) {
        //处理 5.0以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //直接调用系统自带的
            activity.getWindow().setStatusBarColor( color );
        }
        //处理 4.4 - 5.0, 没办法调用上面的方法, 需要用一个技巧
        //先设置为全屏, 然后在状态栏的位置加一个布局, 代替状态栏.
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //先设置为全屏透明
            activity.getWindow().addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
            //创建自己的 statusBar
            View view = new View( activity );
            view.setLayoutParams( new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight( activity ) ) );
            view.setBackgroundColor( color );
            //将我们创建的statusBar 赋值给 viewGroup
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            decorView.addView( view, 0 );

            //拿到 根布局中的 contentView
            ViewGroup contentView = activity.findViewById( android.R.id.content );
            //获取到我们 set 进去的布局
            View activityView = contentView.getChildAt( 0 );
            //设置属性
            activityView.setFitsSystemWindows( true );

        }
    }

    private static int getStatusBarHeight(Activity activity) {
        Resources rs = activity.getResources();
        //获取资源ID
        int statusBarHeightId = rs.getIdentifier( "status_bar_height", "dimen", "android" );
        //再根据ID 获取资源
        return rs.getDimensionPixelOffset( statusBarHeightId );
    }

    /**
     * 设置 Activity 全屏
     *
     * @param activity
     */
    public static void setActivityTranslucent(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = activity.getWindow().getDecorView();
            decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN );
            activity.getWindow().setStatusBarColor( Color.TRANSPARENT );
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
        }
    }

    public static void fixSystemUI(Activity mActivity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //获取最顶层的View
            mActivity.getWindow().getDecorView()
                    .setSystemUiVisibility(
                            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE );
            mActivity.getWindow().setStatusBarColor( Color.TRANSPARENT );
        }
    }


}